package com.example.italkapp2.controller.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.example.italkapp2.R;
import com.example.italkapp2.controller.adapter.PickContactAdapter;
import com.example.italkapp2.model.Model;
import com.example.italkapp2.model.bean.PickContactInfo;
import com.example.italkapp2.model.bean.UserInfo;
import com.example.italkapp2.utils.Constant;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;

import java.util.ArrayList;
import java.util.List;

//选择联系人页面
public class PickContactActivity extends Activity {
    private Button btn_pick_save;
    private Button btn_return;
    private ListView lv_pick;
    private List<PickContactInfo> mPicks=new ArrayList<>();
    private PickContactAdapter pickContactAdapter;
    private List<String> mExistMembers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_contact);

        //获取传递过来的数据
        getData();
        initView();
        initData();
        initListener();
    }

    private void getData() {
        String groupId = getIntent().getStringExtra(Constant.GROUP_ID);
        if(groupId!=null){
            EMGroup group = EMClient.getInstance().groupManager().getGroup(groupId);
            mExistMembers = group.getMembers();
        }

        if(mExistMembers==null){
            mExistMembers=new ArrayList<>();
        }
    }

    private void initListener() {
        //checkbox的点击事件
        lv_pick.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //checkbox的切换
                CheckBox checkBox = view.findViewById(R.id.cb_pick);
                checkBox.setChecked(!checkBox.isChecked());

                //修改数据
                PickContactInfo pickContactInfo = mPicks.get(position);
                pickContactInfo.setChecked(checkBox.isChecked());

                //刷新页面
                pickContactAdapter.notifyDataSetChanged();
            }
        });

        //保存按钮的点击事件
        btn_pick_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取到已经选择的联系人
                List<String> names=pickContactAdapter.getPickContacts();

                //给启动页面返回数据
                Intent intent = new Intent();
                intent.putExtra("members",names.toArray(new String[0]));

                //设置结果码
                setResult(RESULT_OK,intent);

                //结束当前页面
                finish();
            }
        });

        btn_return.setOnClickListener(v -> PickContactActivity.this.finish());
    }

    private void initData() {
        //获取所有联系人的信息
        List<UserInfo> contacts = Model.getInstance().getDbManager().getContactTableDao().getContacts();
        if(contacts!=null&&contacts.size()>=0){
            for(UserInfo contact:contacts){
                PickContactInfo pickContactInfo = new PickContactInfo(contact, false);
                mPicks.add(pickContactInfo);
            }
        }

        pickContactAdapter = new PickContactAdapter(this,mPicks,mExistMembers);
        lv_pick.setAdapter(pickContactAdapter);
    }

    private void initView() {
        btn_pick_save=findViewById(R.id.btn_pick_save);
        lv_pick=findViewById(R.id.lv_pick);
        btn_return=findViewById(R.id.btn_return);
    }
}